<?php

namespace DC\ServiceCenter\Http\Middleware;

use App\Helpers\AppSecret;
use Closure;
use Illuminate\Http\Request;

class ServiceAuth
{
    public function handle(Request $request, Closure $next)
    {
        if(!$this->checkSecret($request)){
            return response()->json(['status'=>0,'msg'=>'未授权访问']);
        }
        return $next($request);
    }

    public function checkSecret(Request $request)
    {
        $client_secret = $request->header("secret",false);
        $request_time = $request->header('time',time());
        $token = $request->header('token',false);
        $server_secret = AppSecret::make()->create($token,$request_time);
        if($client_secret!=$server_secret){
            return false;
        }
        return true;
    }
}